package com.yiyuan.app.utils;

import com.aliyun.cloudauth20190307.Client;
import com.aliyun.cloudauth20190307.models.Id2MetaVerifyRequest;
import com.aliyun.cloudauth20190307.models.Id2MetaVerifyResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.yiyuan.app.config.AliAccessKeyProperties;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;

@Slf4j
public class Id2MetaVerifyUtils {


    public static Id2MetaVerifyResponse id2MetaVerifyAutoRoute(Id2MetaVerifyRequest request, AliAccessKeyProperties properties) {
        // 第一个为主区域Endpoint，第二个为备区域Endpoint。
        List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com", "cloudauth.cn-beijing.aliyuncs.com");
        Id2MetaVerifyResponse lastResponse = null;
        for (int i = 0; i < endpoints.size(); i++) {
            try {
                Id2MetaVerifyResponse response = id2MetaVerify(endpoints.get(i), request, properties);
                lastResponse = response;

                // 服务端错误，切换到下个区域调用。
                if (response != null) {
                    if (500 == response.getStatusCode()) {
                        continue;
                    }
                    if (response.getBody() != null) {
                        if ("500".equals(response.getBody().getCode())) {
                            continue;
                        }
                    }
                }

                return response;
            } catch (Exception e) {
                log.error("{}校验实名信息异常：{}", endpoints.get(i), e);
                if (i == endpoints.size() - 1) {
                    throw new RuntimeException(e);
                }
            }
        }

        return lastResponse;
    }

    private static Id2MetaVerifyResponse id2MetaVerify(String endpoint, Id2MetaVerifyRequest request, AliAccessKeyProperties properties)
            throws Exception {
        // 使用AK 初始化Credentials Client。
        com.aliyun.credentials.models.Config credentialsConfig = new com.aliyun.credentials.models.Config();
        // 凭证类型。
        credentialsConfig.setType("access_key");
        // 设置为AccessKey ID值。
        credentialsConfig.setAccessKeyId(properties.getAccessKeyId());
        // 设置为AccessKey Secret值。
        credentialsConfig.setAccessKeySecret(properties.getAccessKeySecret());
        com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client(credentialsConfig);
        Config config = new Config();
        config.setCredential(credentialClient);
        config.setEndpoint(endpoint);
        // 设置http代理。
        //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
        // 设置https代理。
        //config.setHttpsProxy("http://xx.xx.xx.xx:xxxx");
        Client client = new Client(config);

        // 创建RuntimeObject实例并设置运行参数。
        RuntimeOptions runtime = new RuntimeOptions();
        runtime.readTimeout = 5000;
        runtime.connectTimeout = 5000;

        return client.id2MetaVerifyWithOptions(request, runtime);
    }
}
